#include "chatitembase.h"
#include "chatpage.h"
#include "picturebubble.h"
#include "tcpmgr.h"
#include "textbubble.h"
#include "ui_chatpage.h"
#include "usermgr.h"

#include <QPainter>
#include <QStyleOption>
#include<QDebug>
#include <QUuid>
#include <QJsonDocument>
ChatPage::ChatPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatPage)
{
    ui->setupUi(this);
    //设置按钮样式
    ui->receive_btn->SetState("normal","hover","press");
    ui->send_btn->SetState("normal","hover","press");

    //设置图标样式
    ui->emoLb->SetState("normal","hover","press","normal","hover","press");
    ui->fileLb->SetState("normal","hover","press","normal","hover","press");

    connect(ui->send_btn,&QPushButton::clicked,this,[=](){
        on_btn_clicked(true);
    });
    connect(ui->receive_btn,&QPushButton::clicked,this,[=](){
        on_btn_clicked(false);
    });
    ui->chatEidt->installEventFilter(this);

}

ChatPage::~ChatPage()
{
    delete ui;
}

void ChatPage::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

bool ChatPage::eventFilter(QObject *obj, QEvent *e)
{

    if (e->type() == QEvent :: KeyPress && obj == ui->chatEidt)
    {

        QKeyEvent *event = static_cast <QKeyEvent *> (e) ;
        if (event->key() == Qt :: Key_Return || event->key() == Qt :: Key_Enter)
        {
            if(ui->chatEidt->document()->isEmpty()){
                qDebug()<<"内容为空";
                return false;
            }
            emit on_btn_clicked(true);
            return true ;
        }
    }
    return QObject::eventFilter(obj,e) ;
}

void ChatPage::AppendChatMsg(std::shared_ptr<TextChatData> msg)
{
    auto self_info = UserMgr::GetInstance()->GetUserInfo();
    ChatRole role;
    //todo... 添加聊天显示
    if (msg->_from_uid == self_info->_uid) {
        role = ChatRole::Self;
        ChatItemBase* pChatItem = new ChatItemBase(role);

        pChatItem->setUserName(self_info->_name);
        pChatItem->setUserIcon(QPixmap(self_info->_icon));
        QWidget* pBubble = nullptr;
        pBubble = new TextBubble(role, msg->_msg_content);
        pChatItem->setWidget(pBubble);
        ui->chatDataList->appendChatItem(pChatItem);
    }
    else {
        role = ChatRole::Other;
        ChatItemBase* pChatItem = new ChatItemBase(role);
        auto friend_info = UserMgr::GetInstance()->GetFriendById(msg->_from_uid);
        if (friend_info == nullptr) {
            return;
        }
        pChatItem->setUserName(friend_info->_name);
        pChatItem->setUserIcon(QPixmap(friend_info->_icon));
        QWidget* pBubble = nullptr;
        pBubble = new TextBubble(role, msg->_msg_content);
        pChatItem->setWidget(pBubble);
        ui->chatDataList->appendChatItem(pChatItem);
    }
}

void ChatPage::on_btn_clicked(bool flag)
{
    auto pTextEdit = ui->chatEidt;
    ChatRole role = ChatRole::Self;

    QString userName = QStringLiteral("恋恋风辰");
    QString userIcon = ":/avatar/head_1.jpg";
    if(!flag){
        role = ChatRole::Other;
        userName = QStringLiteral("我是测试");
        userIcon = ":/avatar/head_3.jpg";
    }
    const QVector<MsgInfo>& msgList = pTextEdit->getMsgList();
    for(int i=0; i<msgList.size(); ++i)
    {
        QString type = msgList[i].msgFlag;
        ChatItemBase *pChatItem = new ChatItemBase(role);
        pChatItem->setUserName(userName);
        pChatItem->setUserIcon(QPixmap(userIcon));
        QWidget *pBubble = nullptr;
        if(type == "text")
        {
            pBubble = new TextBubble(role, msgList[i].content);
        }
        else if(type == "image")
        {
            pBubble = new PictureBubble(QPixmap(msgList[i].content) , role);
        }
        else if(type == "file")
        {

        }
        if(pBubble != nullptr)
        {
            pChatItem->setWidget(pBubble);
            ui->chatDataList->appendChatItem(pChatItem);
        }
    }
}

void ChatPage::on_send_btn_clicked()
{
    if (_user_info == nullptr) {
         qDebug() << "friend_info is empty";
         return;
     }

     auto user_info = UserMgr::GetInstance()->GetUserInfo();
     auto pTextEdit = ui->chatEidt;
     ChatRole role = ChatRole::Self;
     QString userName = user_info->_name;
     QString userIcon = user_info->_icon;

     const QVector<MsgInfo>& msgList = pTextEdit->getMsgList();
     QJsonObject textObj;
     QJsonArray textArray;
     int txt_size = 0;

     for(int i=0; i<msgList.size(); ++i)
     {
         //消息内容长度不合规就跳过
         if(msgList[i].content.length() > 1024){
             continue;
         }

         QString type = msgList[i].msgFlag;
         ChatItemBase *pChatItem = new ChatItemBase(role);
         pChatItem->setUserName(userName);
         pChatItem->setUserIcon(QPixmap(userIcon));
         QWidget *pBubble = nullptr;

         if(type == "text")
         {
             //生成唯一id
             QUuid uuid = QUuid::createUuid();
             //转为字符串
             QString uuidString = uuid.toString();

             pBubble = new TextBubble(role, msgList[i].content);
             if(txt_size + msgList[i].content.length()> 1024){
                 textObj["fromuid"] = user_info->_uid;
                 textObj["touid"] = _user_info->_uid;
                 textObj["text_array"] = textArray;
                 QJsonDocument doc(textObj);
                 QByteArray jsonData = doc.toJson(QJsonDocument::Compact);
                 //发送并清空之前累计的文本列表
                 txt_size = 0;
                 textArray = QJsonArray();
                 textObj = QJsonObject();
                 //发送tcp请求给chat server
                 emit TcpMgr::GetInstance()->SigSendData(ReqId::ID_TEXT_CHAT_MSG_REQ, jsonData);
             }

             //将bubble和uid绑定，以后可以等网络返回消息后设置是否送达
             //_bubble_map[uuidString] = pBubble;
             txt_size += msgList[i].content.length();
             QJsonObject obj;
             QByteArray utf8Message = msgList[i].content.toUtf8();
             obj["content"] = QString::fromUtf8(utf8Message);
             obj["msgid"] = uuidString;
             textArray.append(obj);
             auto txt_msg = std::make_shared<TextChatData>(uuidString, obj["content"].toString(),
                 user_info->_uid, _user_info->_uid);
             emit sig_append_send_chat_msg(txt_msg);
         }
         else if(type == "image")
         {
              pBubble = new PictureBubble(QPixmap(msgList[i].content) , role);
         }
         else if(type == "file")
         {

         }
         //发送消息
         if(pBubble != nullptr)
         {
             pChatItem->setWidget(pBubble);
             ui->chatDataList->appendChatItem(pChatItem);
         }

     }

     qDebug() << "textArray is " << textArray ;
     //发送给服务器
     textObj["text_array"] = textArray;
     textObj["fromuid"] = user_info->_uid;
     textObj["touid"] = _user_info->_uid;
     QJsonDocument doc(textObj);
     QByteArray jsonData = doc.toJson(QJsonDocument::Compact);
     //发送并清空之前累计的文本列表
     txt_size = 0;
     textArray = QJsonArray();
     textObj = QJsonObject();
     //发送tcp请求给chat server
     emit TcpMgr::GetInstance()->SigSendData(ReqId::ID_TEXT_CHAT_MSG_REQ, jsonData);
}
